package com.ruoyi.order.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.constant.OrderConstants;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.edu.api.domain.EduCourse;
import com.ruoyi.order.api.domain.TOrder;
import com.ruoyi.order.api.feign.IOrderClient;
import com.ruoyi.order.service.ITOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Alex
 * @version 1.0
 * @date 2023/9/20 15:16
 */
@RestController
public class OrderClient implements IOrderClient {

    @Autowired
    private ITOrderService tOrderService;

    /**
     * 远程调用，edu模块获取课程详情时检查会员是否已经购入该课程
     *
     * @param memberId 会员id
     * @param courseId 课程id
     * @return 会员是否已经购入该课程
     */
    @Override
    @RequestMapping("/order/order/queryBuy/{memberId}/{courseId}")
    public AjaxResult queryBuy(@PathVariable("memberId") String memberId, @PathVariable("courseId") String courseId) {
        LambdaQueryWrapper<TOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TOrder::getCourseId, courseId);
        queryWrapper.eq(TOrder::getMemberId, memberId);
        TOrder one = tOrderService.getOne(queryWrapper);
        // 订单不存在或未支付时返回false
        if (one == null || OrderConstants.UN_PAY.equals(one.getStatus())) {
            return AjaxResult.success(false);
        }
        return AjaxResult.success(true);
    }
}
